SRC =  $(wildcard *.cpp)
OBJ = $(addprefix $(BUILD)/,$(patsubst %.cpp,%$(OBJ_SFX),$(SRC)))

DEPDIR = $(BUILD)
df = $(DEPDIR)/$(*F)

PFX := $(subst /,\/,$(BUILD)/)

MAKEDEPEND = $(CPP) $(CPPFLAGS) $< \
                 | sed -n 's/^\#$(LINE) *[0-9][0-9]* *"\([^"]*\)".*/$(PFX)$*$(OBJ_SFX): \1/p' \
		 | sed '/.*<.*>/d' \
                 | $(SORT) -u > $(BUILD)/$*.d


compile: $(BUILD) $(OBJ)


$(BUILD)/%$(OBJ_SFX): %.cpp
	@$(MAKEDEPEND);
	cp $(df).d $(df).P; \
	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	    -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
	rm -f $(df).d
	$(CC) $(CFLAGS) $(CPPFLAGS) $(COMP) $< $(OUT)$@

-include $(SRC:%.cpp=$(BUILD)/%.P)

$(BUILD):
	mkdir $(BUILD)

.PHONY: clean 

gclean:
	rm -f $(BUILD)/*$(OBJ_SFX)
	rm -f $(BUILD)/*.[asP]
	rm -f $(BUILD)/*~


printvars:
	@echo 'BUILD:	$(BUILD)'
	@echo 'SRC:	$(SRC)'
	@echo 'OBJ:	$(OBJ)'
	@echo 'CFLAGS:	$(CFLAGS)'
	@echo 'df:	$(df)'